$().ready(function() {

	$("#guessStartDate").datetimepicker({
		format : 'YYYY-MM-DD HH:mm:ss',
	});

	//控制开始时间大于等于当天时间
	$("#guessEndDate").datetimepicker({
		format : 'YYYY-MM-DD HH:mm:ss',
	});

	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
function update() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/system/guess/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			guessTitle : {
				required : true,
				maxlength: 100
			},
			guessStartDate: {
				required : true,
			},
			guessEndDate: {
				required : true,
			}
		},
		messages : {
			guessTitle : {
				required : icon + "请输入竞猜标题",
				maxlength: icon + "竞猜标题不能超过100个字符",
			},
			guessStartDate : {
				required : icon + "请输入竞猜开始时间",
			},
			guessEndDate : {
				required : icon + "请输入竞猜结束时间",
			}
		}
	})
}

//加载已经添加的竞猜项目
loadGuessItem();

var i = 0;
function loadGuessItem() {
	var id = $("#id").val();
	var html = '';
	$.ajax({//发送AJAX请求
		type : "POST",
		url : "/system/guess/loadGuessItem?guessId="+id,
		success : function(data) {
			if(data != null && data != '') {
				data = data.data;
				$.each(data, function(){
					i++;
					html += '<tr>';
					html += '<td style="text-align: center;">'+i+'</td>';
					html += '<td style="text-align: center;" id="selected'+i+'">';
					html += '<input maxlength="50" id="guessItemDOList['+(i-1)+'].id" name="guessItemDOList['+(i-1)+'].id" class="form-control" type="hidden" value="'+this.id+'" required>';
					html += '<input maxlength="50" id="guessItemDOList['+(i-1)+'].guessName" name="guessItemDOList['+(i-1)+'].guessName" class="form-control" type="text" value="'+this.guessName+'" required>';
					html += '</td>';
					/*html += '<td style="text-align: center;">';
					html += '<input maxlength="50" id="guessItemDOList['+(i-1)+'].guessOdds" name="guessItemDOList['+(i-1)+'].guessOdds" class="form-control" type="text" value="'+this.guessOdds+'" required>';
					html += '</td>';*/
					html += '</tr>';
				});
				$("#tbody1").append(html);
			}
		}
	});
}

function addGuessItem() {
	i++;
	var html = '';
	html += '<tr>';
	html += '<td style="text-align: center;">'+i+'</td>';
	html += '<td style="text-align: center;" id="selected'+i+'">';
	html += '<input maxlength="50" id="guessItemDOList['+(i-1)+'].guessName" name="guessItemDOList['+(i-1)+'].guessName" class="form-control" type="text" required>';
	html += '</td>';
	/*html += '<td style="text-align: center;">';
	html += '<input maxlength="10" id="guessItemDOList['+(i-1)+'].guessOdds" name="guessItemDOList['+(i-1)+'].guessOdds" class="form-control" type="text" required>';
	html += '</td>';*/
	html += '</tr>';
	$("#tbody1").append(html);
}